Skip to content

Implemented token renewal system#455

Merged
truthixify merged 3 commits intoDistinctCodes:mainfrom
Lansa-18:feature/token-renewal
Jan 30, 2026
Merged

Implemented token renewal system#455
truthixify merged 3 commits intoDistinctCodes:mainfrom
Lansa-18:feature/token-renewal

Conversation

@Lansa-18
Copy link
Contributor

Token Renewal System Implementation

Issue Reference

Closes #421

Overview

Implementation of the Token Renewal System for ManageHub membership tokens, including automatic renewal options, grace periods for expired tokens, and comprehensive renewal tracking.

Status: ✅ Complete and Tested (51/51 tests passing)


Files Modified

  1. contracts/manage_hub/src/membership_token.rs - Renewal functions and logic
  2. contracts/manage_hub/src/lib.rs - Renewal contract endpoints
  3. contracts/manage_hub/src/types.rs - Renewal data structures
  4. contracts/manage_hub/src/errors.rs - Renewal-specific errors
  5. contracts/common_types/src/types.rs - Grace period status enum
  6. contracts/manage_hub/src/subscription.rs - Made USDC getter public
  7. contracts/manage_hub/src/test.rs - Comprehensive test suite (15 new tests)

Acceptance Criteria Implemented

✅ 1. Add renew_token function to membership contract

  • Implemented renew_token() function with tier-based pricing integration
  • Validates renewals are enabled via configuration
  • Fetches tier pricing dynamically (Monthly/Annual billing cycles)
  • Validates payment token (USDC requirement)
  • Smart expiry extension: extends from current expiry if active, from current time if expired
  • Clears grace period fields on successful renewal
  • Tracks renewal attempts counter
  • Records renewal in history
  • Emits token_rnw event with payment details

✅ 2. Implement grace period for expired tokens (configurable, default 7 days)

  • Implemented check_and_apply_grace_period() function
  • Automatically transitions expired Active tokens to GracePeriod status
  • Configurable grace period duration (default: 7 days / 604,800 seconds)
  • Sets grace period entry and expiry timestamps
  • Added GracePeriod variant to MembershipStatus enum
  • Tokens remain usable during grace period but transfers are blocked
  • Modified transfer_token() to prevent transfers during grace period
  • Emits grace_in event when entering grace period
  • Returns GracePeriodExpired error if period has lapsed

✅ 3. Add automatic renewal configuration and checking

  • Implemented set_renewal_config() (admin-only) and get_renewal_config() functions
  • Created RenewalConfig struct with configurable settings:
    • Grace period duration (default: 7 days)
    • Auto-renewal notice period (default: 1 day)
    • Renewals enabled flag
  • Implemented check_auto_renewal_eligibility() function
  • Checks if token is within renewal window (1 day before expiry by default)
  • Created AutoRenewalSettings struct for user preferences
  • Implemented set_auto_renewal() and get_auto_renewal_settings() for user control
  • Emits rnw_cfg event on configuration updates

✅ 4. Include payment validation for renewals with configurable fees

  • Integrated with existing subscription module tier pricing system
  • Dynamically fetches tier pricing at renewal time
  • Validates payment token matches USDC contract address
  • Validates payment amount is positive
  • Supports Monthly and Annual billing cycles with different pricing
  • Monthly: 30 days extension
  • Annual: 365 days extension
  • Follows existing payment validation pattern from subscription module

✅ 5. Add renewal history tracking

  • Implemented record_renewal() internal function
  • Created RenewalHistory struct tracking:
    • Timestamp, tier ID, amount, payment token
    • Success/failure status
    • Trigger reason (Manual/AutoRenewal/GracePeriod)
    • Old and new expiry dates
    • Error messages on failure
  • Created RenewalTrigger enum for tracking renewal source
  • Implemented get_renewal_history() function for retrieval
  • History stored per-token in persistent storage with TTL extension

✅ 6. Update tests for renewal scenarios including edge cases

Implemented 15 comprehensive test cases:

  • Configuration management tests
  • Successful renewal with tier pricing
  • Error handling (tier not found, grace period expired)
  • Grace period functionality and transfer restrictions
  • Renewal history tracking with multiple renewals
  • Auto-renewal settings persistence
  • Auto-renewal eligibility window detection
  • Smart expiry extension (before vs after expiry scenarios)
  • Grace period clearing on successful renewal

All 51 tests passing successfully

✅ 7. Add renewal eligibility checking functions

  • Implemented check_auto_renewal_eligibility() function
  • Checks three conditions:
    1. Current time ≥ (expiry_date - auto_renewal_notice_days)
    2. Current time < expiry_date
    3. Token status == Active
  • Used by external services to identify tokens ready for auto-renewal
  • Used internally by process_auto_renewal() before processing

✅ 8. Implement renewal notification events

Implemented 6 event types:

  1. rnw_cfg - Renewal configuration updated
  2. token_rnw - Token successfully renewed
  3. grace_in - Grace period entered
  4. grace_ar - Grace period entered due to auto-renewal failure
  5. auto_ok - Auto-renewal successful
  6. auto_rnw - Auto-renewal settings updated

All events include relevant data (amounts, timestamps, status changes)


Key Implementation Details

Data Structures Added

  • Extended MembershipToken with 5 new fields (tier_id, grace period tracking, renewal attempts)
  • Added 3 new storage keys (RenewalConfig, RenewalHistory, AutoRenewalSettings)
  • Created RenewalConfig, RenewalHistory, AutoRenewalSettings, and RenewalTrigger types

Core Functions Implemented

  • renew_token() - Manual renewal with payment
  • set_renewal_config() / get_renewal_config() - Configuration management
  • check_and_apply_grace_period() - Grace period handling
  • set_auto_renewal() / get_auto_renewal_settings() - Auto-renewal preferences
  • check_auto_renewal_eligibility() - Eligibility checking
  • process_auto_renewal() - Automatic renewal processing
  • get_renewal_history() - History retrieval
  • record_renewal() - Internal history recording

Error Codes Added

  • RenewalNotAllowed (Error 46) - Renewals disabled or conditions not met
  • TransferNotAllowedInGracePeriod (Error 47) - Cannot transfer during grace period
  • GracePeriodExpired (Error 48) - Grace period has lapsed
  • AutoRenewalFailed (Error 49) - Auto-renewal attempt failed

Design Patterns

  • Smart Expiry Extension: Extends from current expiry if not expired, from current time if expired
  • Tier-Based Pricing: Dynamic pricing lookup at renewal time
  • Grace Period Philosophy: Usable but restricted access, transfers blocked
  • Auto-Renewal Strategy: Proactive (1 day before expiry), user opt-in, manual fallback on failure
  • Audit Trail: Complete history with success/failure tracking

Testing Results

  • 51/51 tests passing in manage_hub contract
  • 14/14 tests passing in common_types module
  • 100% coverage of new functionality
  • All acceptance criteria validated
  • Edge cases and error scenarios tested

Integration Points

  • Subscription Module: Tier pricing, USDC validation, billing cycle patterns
  • Token System: Status management, transfer logic, token issuance

@vercel
Copy link

vercel bot commented Jan 30, 2026

@Lansa-18 is attempting to deploy a commit to the naijabuz's projects Team on Vercel.

A member of the Team first needs to authorize it.

@truthixify truthixify merged commit 3997fae into DistinctCodes:main Jan 30, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CONTRACTS: Implement Token Renewal System

2 participants